<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\jwt\Jwts;
use think\exception\ValidateException;
use app\index\validate\Merchant;

class Login extends Controller {

    public function index() {
        $data = request()->param();
        $validate = new Merchant;
        if (!$validate->check($data)) {
            throw new ValidateException($validate->getError());
        }
        $info = pdo_get('member', array('ismerch' => 1, 'mobile' => $data['mobile']));
        if (!$info) {
            throw new ValidateException('手机号错误!');
        }
        if ($info['pwd'] != md5($data['pwd'] . $info['salt'])) {
            throw new ValidateException('手机号或密码错误!');
        }
        if($info['endtime']<time())
        {
            jsonp_error(422,'帐号过期');
        }
        //登录成功
        $res = ['messages'=>'登录成功','data'=>['token'=>Jwts::getToken($info),'exp_time'=>7200],'code'=>200];
        return \json($res);
    }

    public function out() {

        session('merchinfo', null);
        $url = $this->request->domain(true) . '/index/login';
        header('HTTP/1.1 303 See Other');
        header("Location: $url");
        exit;
    }

}
